<%@page import="java.util.TreeMap"%>
<%@page import="java.util.Map"%>
<%@page import="entities.MetaSeo"%>
<%@page import="systems.ConfigSystem"%>
<%@page import="entities.SiteConfig"%>
<%@page import="helpers.LanguageHelper"%>
<%@page import="helpers.StringHelper"%>
<%@page import="helpers.UrlHelper"%>
<%@page import="com.google.appengine.api.images.ImagesService"%>
<%@page import="com.google.appengine.api.images.ImagesServiceFactory"%>
<%@page import="com.google.appengine.api.blobstore.BlobstoreServiceFactory"%>
<%@page import="com.google.appengine.api.blobstore.BlobstoreService"%>
<%@page import="com.google.appengine.api.blobstore.BlobKey"%>
<%@page import="java.util.List"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="helpers.DateHelper"%>
<%@page import="entities.User"%>
<%@page import="entities.Category"%>
<%@page import="entities.Article"%>
<%
SiteConfig siteConfig = ConfigSystem.getConfigs();
Article article = (Article) request.getAttribute("article");
List<Article> cArticles = (List<Article>) request.getAttribute("cArticles");
List<Article> uArticles = (List<Article>) request.getAttribute("uArticles");
Category category = (Category) request.getAttribute("category");
User author = (User) request.getAttribute("author");
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
ImagesService imagesService = ImagesServiceFactory.getImagesService();
LanguageHelper translate = (LanguageHelper) request.getAttribute("translate");
MetaSeo metaseo = (MetaSeo) request.getAttribute("metaseo");

//Set content for Seo
Map<String, String> replates = new TreeMap<String, String>();
replates.put("__SERVER_NAME__", UrlHelper.getDomainServer(request));
replates.put("__SERVER_URL__", UrlHelper.getServerUrl(request));
replates.put("__ARTICLE_TITLE__", article.getTitle());
replates.put("__ARTICLE_CONTENT__", StringHelper.cutHtmlToText(article.getContent(), 150));
replates.put("__ARTICLE_ALIAS__", article.getAlias());


String title = StringHelper.replateContent(metaseo.getTitle(), replates);
String description = StringHelper.replateContent(metaseo.getDescription(), replates);
String keywords = StringHelper.replateContent(metaseo.getKeywords(), replates);
String url = StringHelper.replateContent(metaseo.getOgurl(), replates);
%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<jsp:include page="/templates/classic/layouts/header.jsp" >
	<jsp:param name="title" value="<%=title %>" />
    <jsp:param name="description" value="<%=description %>" />
    <jsp:param name="keywords" value="<%=keywords %>" />
    <jsp:param name="ogtitle" value="<%=title %>" />
    <jsp:param name="ogurl" value="<%=url %>" />
    <jsp:param name="ogdescription" value="<%=description%>" />
</jsp:include>
		<div id="wrapper">
			
			<div id="page-title">
				<div id="page-title-inner">
					<div class="container">
						<h2>Single Post</h2>
					</div>
				</div>	
			</div>
			
			
			<div class="row-fluid">
				<div class="span8 left-content">
					<article>
						<div class="date-info">
							<span class="day">1</span>
							<span class="month-year">June, 2012</span>
						</div>
						<div class="article-info">
							<div class="article-title">
								<h2><%=article.getTitle() %></h2>
							</div>
						</div>
						<div class="article-meta">
							<span><i class="icon-user"></i> 
							<% if (author != null) { %>
                            &nbsp; <a href="/author/<%=author.getId() %>" itemprop="author"><%=author.getName() %></a> | 
                            <% } else { %>
                            &nbsp; <%=article.getAuthorName() %> |
                            <% } %>
							</span>
							<% if (category != null) { %> 
							<span><i class="icon-folder-open"></i> <a href="/category/<%=category.getAlias() %>"><%=category.getName() %></a></span>
							<% } %>
						</div>
						<div class="article-desc">
							<%=article.getContent() %>
						</div>
						
					</article>
					
					
					 <% if (author != null) { %>
					<div class="author">
						<div class="clearfix">
							<div class="pull-left author-image">
								<a href="/author/<%=author.getId() %>">
									<% 
									if (author.getAvatar() != null && !author.getAvatar().isEmpty())  {
										 BlobKey blobKey = new BlobKey(author.getAvatar());
									%>
									<img alt="" src="<%=imagesService.getServingUrl(blobKey) %>=s60-c"><br />
									<% } else { %>							
									<img class="img-full-width" itemprop="image" src="/templates/classic/images/icon-author.jpg" />
									<% } %>
								</a>
							</div>
							<div class="pull-left author-info">
								<a class="author_name" href="/author/<%=author.getId() %>"><%=author.getName() %></a>
								<span>Joined: <%=DateHelper.getStringFromDate(author.getCreated(), "MMM, dd yyyy") %></span> | 
								<span>Website: <%=author.getWebsite() %></span>
								<p>
								<strong>About Me: </strong> <%=author.getAboutme() %></p>
							</div>
						</div>
					</div>
					<% } %>
					
					<%  if (uArticles != null && uArticles.size() > 0) { %>
					<div class="authors-article">
						<div class="title">
							<h3>Author's Article</h3>
						</div>
						<ul class="">
							<%
                    		for (int i = 0; i < uArticles.size(); i++) {
                    			if (uArticles.get(i).getId().equals(article.getId())) break;
                    		%>
							<li><a href="/article/<%=uArticles.get(i).getAlias() %>"><%=uArticles.get(i).getTitle() %></a></li>
							<% } %>
						</ul>
					</div>
					<br />
					<% } %>
					
					<% if (article.getKeywords().size() > 0) { %>
                    <div class="tag-listing">
                    	<div class="title">
							<h3>Keyword</h3>
						</div>
                    	<% for (int i = 0; i < article.getKeywords().size(); i++) { %>
                    	<%= i > 0 ? ", " : "" %>
                    	<a href="/tag/<%=URLEncoder.encode(article.getKeywords().get(i)) %>" itemprop="keywords"><%=article.getKeywords().get(i) %></a> 
                    	<% } %>
                    </div>
                    <br />
                    <% } %>
					
					<% if (cArticles != null && cArticles.size() > 0) { %>
					<div class="others-article">
						<div class="title">
							<h3>Other articles</h3>
						</div>
						<ul class="">
							<% 
                    		for (int i = 0; i < cArticles.size(); i++) {
                    			if (cArticles.get(i).getId().equals(article.getId())) break;
                    		%>
							<li><a href="/article/<%=cArticles.get(i).getAlias()%>"><%=cArticles.get(i).getTitle()%></a></li>
							<% } %>
						</ul>
					</div>
					<% } %>
				</div>
				
				<jsp:include page="/templates/classic/elements/right.jsp" />
				
			</div>
		</div>
		
<jsp:include page="/templates/classic/layouts/footer.jsp" />